/**
 * @file car_control.c
 * @brief Control functions for a robot car using GPIO pins.
 *
 * This file contains functions for controlling the movement of a robot car
 * based on GPIO pin configurations. The car can move forward, backward, 
 * turn left, turn right, and stop using the appropriate GPIO pins.
 * Each movement function sets specific GPIO pins to control the direction
 * and actions of the wheels.
 *
 * @author Maker Studio
 * @license Apache 2.0
 *
 * This code is open-sourced under the Apache 2.0 license. When using open-source libraries
 * or code, please ensure to acknowledge the original authors and sources.
 */

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "systick.h"


#include "ohos_init.h"
#include "cmsis_os2.h"
#include <unistd.h>
#include "iot_gpio.h"
#include "iot_pwm.h"
#include "iot_errno.h"
#include "iot_watchdog.h"
#include "iot_gpio_ex.h"



#define GPIO6 6
#define GPIO3 3
#define GPIO9 9
#define GPIO7 7
#define GPIOFUNC 0

/**
 * @brief Controls the GPIO pin and sets the output value.
 * 
 * This function initializes the GPIO pin, sets its function, configures its 
 * direction as output, and sets the output value to the specified state.
 * 
 * @param gpio The GPIO pin number.
 * @param value The value to set the GPIO pin to (either HIGH or LOW).
 */
void gpio_control(unsigned int gpio, IotGpioValue value) {
    IoTGpioInit(gpio);  // Initialize the GPIO pin
    IoSetFunc(gpio, GPIOFUNC);  // Set the function of the GPIO pin
    IoTGpioSetDir(gpio, IOT_GPIO_DIR_OUT);  // Set the GPIO pin direction to output
    IoTGpioSetOutputVal(gpio, value);  // Set the output value of the GPIO pin
}

/**
 * @brief Moves the car backward.
 * 
 * This function sets the appropriate GPIO pins to reverse the direction of
 * the wheels and move the car backward.
 *  @author Maker Studio
 * @license Apache 2.0
 */
void car_backward(void) {
    gpio_control(GPIO6, IOT_GPIO_VALUE0);  // Set left motor to reverse
    gpio_control(GPIO3, IOT_GPIO_VALUE1);  // Set left motor to reverse
    gpio_control(GPIO9, IOT_GPIO_VALUE0);  // Set right motor to reverse
    gpio_control(GPIO7, IOT_GPIO_VALUE1);  // Set right motor to reverse
}

/**
 * @brief Moves the car forward.
 * 
 * This function sets the appropriate GPIO pins to move the car forward. 
 * The left and right wheels rotate forward to move the car in the forward direction.
 * @author Maker Studio
 * @license Apache 2.0
 */
void car_forward(void) {
    gpio_control(GPIO6, IOT_GPIO_VALUE1);  // Set left motor to forward
    gpio_control(GPIO3, IOT_GPIO_VALUE0);  // Set left motor to forward
    gpio_control(GPIO9, IOT_GPIO_VALUE1);  // Set right motor to forward
    gpio_control(GPIO7, IOT_GPIO_VALUE0);  // Set right motor to forward
}

/**
 * @brief Turns the car left.
 * 
 * This function sets the GPIO pins to stop the left motor while moving 
 * the right motor forward, making the car turn left.
 * @author Maker Studio
 * @license Apache 2.0
 */
void car_left(void) {
    gpio_control(GPIO6, IOT_GPIO_VALUE0);  // Stop left motor
    gpio_control(GPIO3, IOT_GPIO_VALUE1);  // Stop left motor
    gpio_control(GPIO9, IOT_GPIO_VALUE1);  // Set right motor to forward
    gpio_control(GPIO7, IOT_GPIO_VALUE0);  // Set right motor to forward
}

/**
 * @brief Turns the car right.
 * 
 * This function sets the GPIO pins to stop the right motor while moving 
 * the left motor forward, making the car turn right.
 *  * @author Maker Studio
 * @license Apache 2.0
 */
void car_right(void) {
    gpio_control(GPIO6, IOT_GPIO_VALUE1);  // Set left motor to forward
    gpio_control(GPIO3, IOT_GPIO_VALUE0);  // Set left motor to forward
    gpio_control(GPIO9, IOT_GPIO_VALUE0);  // Stop right motor
    gpio_control(GPIO7, IOT_GPIO_VALUE1);  // Stop right motor
}

/**
 * @brief Stops the car.
 * 
 * This function sets all motors to the brake state by setting the GPIO pins 
 * to high, effectively stopping the car.
 *  * @author Maker Studio
 * @license Apache 2.0
 */
void car_stop(void) {
    gpio_control(GPIO6, IOT_GPIO_VALUE1);  // Stop left motor
    gpio_control(GPIO3, IOT_GPIO_VALUE1);  // Stop left motor
    gpio_control(GPIO9, IOT_GPIO_VALUE1);  // Stop right motor
    gpio_control(GPIO7, IOT_GPIO_VALUE1);  // Stop right motor
}

